/**
 * 报表设计公司(2011)
 */
package com.reportgear.core.swing.pane.base;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import net.abbp.swing.util.GuiUtils;

/**
 * 基本对话框
 * 
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class JBaseDialog extends JDialog {

	/**
	 * 单击确定后的返回结果
	 */
	public static final int RESULT_OK = 0;
	/**
	 * 单击取消后的返回结果
	 */
	public static final int RESULT_CANCEL = 1;
	/**
	 * 控制按钮在下面
	 */
	public static final int CONTROL_BUTTON_BOTTOM = 0;
	/**
	 * 控制按钮靠右
	 */
	public static final int CONTROL_BUTTON_RIGHT = 1;
	// 默认宽度
	private static final int DEFAULT_WIDTH = 400;
	// 默认高度
	private static final int DEFAULT_HEIGHT = 300;
	// 确定按钮
	private JButton btnOK;
	// 取消按钮
	private JButton btnCancel;
	// 返回结果
	private int returnValue = RESULT_CANCEL;
	// 按钮位置
	private int buttonsLayout = CONTROL_BUTTON_BOTTOM;
	// 核心主要面板
	private JPanel basicPane;

	public JBaseDialog() {
		super();
		this.buttonsLayout = CONTROL_BUTTON_BOTTOM;
		initialize();
	}

	/**
	 * 创建对话框，指定所有者、标题、宽度、高度、控制按钮位置
	 * 
	 * @param owner
	 *            所有者
	 * @param title
	 *            标题
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @param buttonsLayout
	 *            控制按钮位置 CONTROL_BUTTON_BOTTOM or CONTROL_BUTTON_RIGHT
	 */
	public JBaseDialog(Window owner, String title, int width, int height, int buttonsLayout) {
		super(owner);
		setTitle(title);
		setSize(width, height);

		if (buttonsLayout < CONTROL_BUTTON_BOTTOM || buttonsLayout > CONTROL_BUTTON_RIGHT) {
			throw new RuntimeException("buttonsLayout must be CONTROL_BUTTON_BOTTOM or CONTROL_BUTTON_RIGHT！");
		}
		this.buttonsLayout = buttonsLayout;
		initialize();
	}

	/**
	 * 创建对话框，指定所有者、标题、宽度、高度，控制按钮位置靠下
	 * 
	 * @param owner
	 *            所有者
	 * @param title
	 *            标题
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 */
	public JBaseDialog(Window owner, String title, int width, int height) {
		super(owner);
		setTitle(title);
		setSize(width, height);

		this.buttonsLayout = CONTROL_BUTTON_BOTTOM;
		initialize();
	}

	/**
	 * 创建对话框，指定所有者与标题，使用默认宽度与高度，控制按钮位置靠下
	 * 
	 * @param owner
	 *            所有者
	 * @param title
	 *            标题
	 */
	public JBaseDialog(Window owner, String title) {
		this(owner, title, DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	/**
	 * 创建对话框，指定所有者，使用默认宽度与高度，标题为空，控制按钮位置靠下
	 * 
	 * @param owner
	 *            所有者
	 */
	public JBaseDialog(Window owner) {
		this(owner, "", DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	/**
	 * 创建对话框，指定所有者、控制按钮位置，使用默认宽度与高度，标题为空
	 * 
	 * @param owner
	 *            所有者
	 * @param buttonsLayout
	 *            控制按钮位置
	 */
	public JBaseDialog(Window owner, int buttonsLayout) {
		this(owner, "", DEFAULT_WIDTH, DEFAULT_HEIGHT, buttonsLayout);
	}

	/**
	 * 显示对话框,指定显示内容
	 * 
	 * @param owner
	 *            所有者
	 * @param workPane
	 *            工作面板
	 * @param title
	 *            标题
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @param buttonsLayout
	 *            控制按钮位置
	 * @return 执行结果
	 */
	public static int ShowDialog(Window owner, JBasePane workPane, String title, int width, int height,
			int buttonsLayout) {
		JBaseDialog dlg = new JBaseDialog(owner, title, width, height, buttonsLayout);
		dlg.add(workPane, BorderLayout.CENTER);
		dlg.basicPane = workPane;
		dlg.showModel();

		return dlg.returnValue;
	}

	/**
	 * 获取基本面板
	 * 
	 * @return
	 */
	public JPanel getBasicPane() {
		return this.basicPane;
	}

	// 初始化对话框
	private void initialize() {
		JPanel container = (JPanel) getContentPane();
		container.setBorder(BorderFactory.createEmptyBorder(2, 4, 4, 4));
		container.setLayout(new BorderLayout(0, 4));
		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent windowEvent) {
				doCancel();
			}
		});
		InputMap inputMap = container.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		ActionMap actionMap = container.getActionMap();
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "dialogExit");
		actionMap.put("dialogExit", new AbstractAction() {

			public void actionPerformed(ActionEvent evt) {
				doCancel();
			}
		});

		if (this.buttonsLayout == CONTROL_BUTTON_BOTTOM) {
			add(createControlButtonBottomPane(), BorderLayout.SOUTH);
		} else if (this.buttonsLayout == CONTROL_BUTTON_RIGHT) {
			add(createControlButtonRightPane(), BorderLayout.EAST);
		}
	}

	/**
	 * 创建控制按钮靠下面板
	 * 
	 * @return 控制按钮面板
	 */
	protected JPanel createControlButtonBottomPane() {
		JPanel paneButtons = new JPanel();
		paneButtons.setLayout(new BorderLayout());
		JPanel pane = new JPanel();
		paneButtons.add(pane, BorderLayout.EAST);
		pane.setLayout(new GridLayout(1, 2, 6, 0));
		this.btnOK = new JButton("确定(O)");
		this.btnOK.setMnemonic('O');
		pane.add(this.btnOK);
		this.btnOK.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				doOK();
			}
		});
		this.btnCancel = new JButton("取消(C)");
		this.btnCancel.setMnemonic('C');
		pane.add(this.btnCancel);
		this.btnCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				doCancel();
			}
		});

		getRootPane().setDefaultButton(this.btnOK);

		return paneButtons;
	}

	/**
	 * 创建控制按钮靠右面板
	 * 
	 * @return 控制按钮面板
	 */
	protected JPanel createControlButtonRightPane() {
		JPanel paneButtons = new JPanel();
		paneButtons.setLayout(new BorderLayout());
		JPanel pane = new JPanel();
		paneButtons.add(pane, BorderLayout.SOUTH);
		pane.setLayout(new GridLayout(2, 1, 0, 6));

		this.btnOK = new JButton("确定(O)");
		this.btnOK.setMnemonic('O');
		pane.add(this.btnOK);
		this.btnOK.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				doOK();
			}
		});

		this.btnCancel = new JButton("取消(C)");
		this.btnCancel.setMnemonic('C');
		pane.add(this.btnCancel);
		this.btnCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				doCancel();
			}
		});

		getRootPane().setDefaultButton(this.btnOK);

		return paneButtons;
	}

	/**
	 * 确定按钮事件
	 */
	public void doOK() {
		setReturnValue(RESULT_OK);
		close();
	}

	/**
	 * 取消按钮事件
	 */
	public void doCancel() {
		setReturnValue(RESULT_CANCEL);
		close();
	}

	/**
	 * 关闭对话框
	 */
	public void close() {
		setVisible(false);
		dispose();
	}

	/**
	 * 模态显示
	 */
	public void showModel() {
		setModal(true);
		GuiUtils.centerWindow(this);
		setVisible(true);
	}

	/**
	 * 获取返回值
	 * 
	 * @return
	 */
	public int getReturnValue() {
		return returnValue;
	}

	/**
	 * 设置返回值
	 * 
	 * @param returnValue
	 *            返回值
	 */
	public void setReturnValue(int returnValue) {
		this.returnValue = returnValue;
	}

	/**
	 * 设置取消返回值
	 */
	public void setCancelReturn() {
		this.returnValue = RESULT_CANCEL;
	}

	/**
	 * 设置确定返回值
	 */
	public void setOKReturn() {
		this.returnValue = RESULT_OK;
	}
}